/**
 * package for all the model classes to fit the mvc format
 */
package javamonsters.model;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

/**
 * The Planet class represents a visitable planet within a solar system.
 * 
 * @author wa
 * @version 1.0
 */
public class Planet implements java.io.Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5627949204899726446L;
	
	/**
	 * object output stream
	 */
	private ObjectOutputStream out;
	
	/**
	 * The name of the planet.
	 */
	private final String name;
	
	/**
	 * marketplace of the planet
	 */
	private final Marketplace mktplace;
	
	/**
	 * parent system of the planet
	 */
	private final SolarSystem parentSystem;
	
	/**
	 * shipYard of the planet
	 */
	private final ShipYard shipYard;
	
	/**
	 * Instantiates a new planet.
	 *
	 * @param name the name of the planet
	 * @param parentSystem the parent system of the planet
	 */
	public Planet(SolarSystem parentSystem, String name) {
		
		this.name = name;
		this.shipYard = new ShipYard(this);
		this.mktplace = new Marketplace(this);
		this.parentSystem = parentSystem;
		out = null;
	}
	
	/**
	 * @return String representation of Planet
	 */
	public String toString() {
		return "planet";
	}
	
	/**
	 * Retrieve the TechLevel for this Planet
	 * @return the TechLevel for this Planet
	 */
	public SolarSystem.TechLevels getTechLevel() {
		return parentSystem.getTechLevel();
	}
	
	/**
	 * Retrieve the Attributes for this Planet
	 * @return the Attributes for this Planet
	 */
	public SolarSystem.Attributes getAttributes() {
		return parentSystem.getAttributes();
	}

	/**
	 * Gets the name of the planet.
	 *
	 * @return the name of the planet
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Retrieve the SolarSystem for this Planet
	 * @return the SolarSystem for this Planet
	 */
	public SolarSystem getSystem() {
		return parentSystem;
	}
	
	/**
	 * Retrieve the Marketplace for this Planet
	 * @return the Marketplace for this Planet
	 */
	public Marketplace getMarketplace() {
		return mktplace;
	}
	
	/**
	 * @return returns the shipYard of the planet
	 */
	public ShipYard getShipYard() {
		return shipYard;
	}
	
	/**
	 * Writes this object to a save file
	 * 
	 * @param filename name of the file to be saved to
	 * @throws IOException 
	 */
	public void writeObject(String filename) throws IOException {
		try {
			out = new ObjectOutputStream(
				  new FileOutputStream(filename + ".dat"));
			
			out.writeObject(this);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			out.close();
		}
	}
}
